package com.nike.common;

import org.beetl.ext.jfinal.BeetlRenderFactory;

import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.render.ViewType;
import com.nike.controller.BrandController;
import com.nike.controller.IndexController;
import com.nike.controller.ProductController;
import com.nike.controller.ProductTypeController;
import com.nike.controller.ProductAttributeController;
import com.nike.controller.ProductcategoryController;
import com.nike.model.Brand;
import com.nike.model.Menu;
import com.nike.model.Product;
import com.nike.model.ProductProductattributemapstore;
import com.nike.model.Productattribute;
import com.nike.model.Productcategory;
import com.nike.model.Producttype;
import com.nike.model.core.DbDialectFactory;
import com.nike.utils.StringUtils;

/**
 * JFinalConfig.
 * <p>
 * Config order: configConstant(), configRoute(), configPlugin(), configInterceptor(), configHandler()
 */
public class MainConfig extends JFinalConfig {

	/**
	 * Config constant
	 */
	@Override
	public void configConstant(Constants me) {

		PropKit.use("config.properties");
		onJfinalStartBefore();
		me.setDevMode(PropKit.getBoolean("devMode"));// 设置开发模式
		me.setViewType(ViewType.JSP);// 设置默认视图类型 默认freemarker
		me.setUrlParaSeparator("&");// 设置URL参数的分割符
		me.setBaseViewPath("/view");//设置默认视图层访问路劲
		// me.setBaseDownloadPath(baseDownloadPath);//设置下载文件的默认路径
		 //me.setBaseUploadPath(baseUploadPath);//设置上传文件的默认路劲
		// me.setBaseViewPath(baseViewPath);//设置路由视图默认路径
		//me.setEncoding("");//设置编码模式
		// me.setErrorView(401, errorView);
		 me.setError404View("/404.html");
		 me.setError500View("/404.html");
		// me.setErrorRenderFactory(errorRenderFactory);//设置默认错误视图工厂

		// me.setFreeMarkerTemplateUpdateDelay(delayInSeconds);//设置freeMark模板引擎的更新时间
		// me.setFreeMarkerViewExtension("");//设置freeMark的默认后缀，默认html
		// me.setJsonFactory(jsonFactory);
		// me.setJspViewExtension(jspViewExtension);//设置JSP的默认后缀
		//设置beetl
		 me.setMainRenderFactory(new BeetlRenderFactory());
		// me.setMaxPostSize(maxPostSize);//设置文件上传大小
		// me.setReportAfterInvocation(true);//设置 devMode 之下的 action report 是否在
		// invocation 之后，默认值为 true
		// me.setTokenCache(tokenCache);//防止重复提交

	}

	/**
	 * Config Route
	 */
	@Override
	public void configRoute(Routes me) {

		// 指定XXX.do的路径 
		me.add("/", IndexController.class);
		me.add("/brand", BrandController.class);
		me.add("/productType", ProductTypeController.class);
		me.add("/productAttribute", ProductAttributeController.class);
		me.add("/productCategory", ProductcategoryController.class);
		me.add("/product", ProductController.class);
		// 路由拆分
		//me.add(new BrandRoute());
	}
	

	/**
	 * Jfinal启动之前运行
	 */
	public void onJfinalStartBefore() {
		dbDialectConfig();
	}
	/**
	 * Config Plugin
	 */
	@Override
	public void configPlugin(Plugins plugins) {

		C3p0Plugin c3p0Plugin = new C3p0Plugin(PropKit.get("jdbc.url"), PropKit.get("jdbc.username"), PropKit.get("jdbc.passwd"));
		ActiveRecordPlugin av = new ActiveRecordPlugin(c3p0Plugin);
		av.setShowSql(true);
		/**在此添加数据库 表-model 映射*/
		av.addMapping("menu", Menu.class);
		av.addMapping("brand", Brand.class);
		av.addMapping("producttype", Producttype.class);
		av.addMapping("productattribute", Productattribute.class);
		av.addMapping("productcategory", Productcategory.class);
		av.addMapping("product", Product.class);
		av.addMapping("product_productattributemapstore", ProductProductattributemapstore.class);
		/**-------------------------------------------------------*/
		plugins.add(c3p0Plugin);
		plugins.add(av);
	}
	
	public DruidPlugin createDruidPlugin() {
		return DbDialectFactory.getDbDialect().createDuidPlugin("loclhost", "3366", "test", "root", "111111");
	}

	@Override
	public void configInterceptor(Interceptors me) {

	}

	@Override
	public void configHandler(Handlers me) {

	}
	
	private void dbDialectConfig() {
		String dialect = PropKit.get("jpress_db_dialect");
		if (StringUtils.isNotBlank(dialect)) {
			//DbDialectFactory.use(dialect);
			DbDialectFactory.use();
		}
	}

}
